package com.wang.store.controller;

import com.wang.store.controller.ex.*;
import com.wang.store.service.ex.*;
import com.wang.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpSession;

//表示控制层的基类
public class BaseController {
    /** 操作成功的状态码 */
    public static final int OK = 200;

    /** @ExceptionHandler用于统一处理方法抛出的异常 */
    @ExceptionHandler({ServiceException.class,FileUploadException.class})
    public JsonResult<Void> handleException(Throwable e) {
        JsonResult<Void> result = new JsonResult<Void>(e);
        if (e instanceof UsernameDuplicateException) {
            result.setState(4000);
            result.setMessage("用户名重复的异常");
        } else if (e instanceof UserNotFoundException) {
            result.setState(4001);
            result.setMessage("用户没有找到");
        }else if (e instanceof PasswordNotmatchException) {
            result.setState(4002);
            result.setMessage("密码不正确的异常");
        }else if (e instanceof AddressCountLimitException) {
            result.setState(4003);
            result.setMessage("用户收货地址超出上限的异常");
        }else if (e instanceof AddressNotFoundException) {
            result.setState(4004);
            result.setMessage("收货地址不存在的异常");
        }else if (e instanceof AccessDeniedException) {
            result.setState(4005);
            result.setMessage("用户收货地址非法访问的异常");
        }else if (e instanceof ProductNotFoundException) {
            result.setState(4006);
            result.setMessage("需要的数据没有找到");
        }else if (e instanceof CartNotFoundException) {
            result.setState(4007);
        }else if (e instanceof InsertException) {
            result.setState(5000);
            result.setMessage("插入时出现未知错误的异常");
        }else if (e instanceof UpdateException) {
            result.setState(5001);
            result.setMessage("更新时出现未知错误的异常");
        }else if (e instanceof DeleteException) {
            result.setState(5002);
            result.setMessage("删除时出现未知错误的异常");
        }else if (e instanceof FileEmptyException) {
            result.setState(6000);
        } else if (e instanceof FileSizeException) {
            result.setState(6001);
        } else if (e instanceof FileTypeException) {
            result.setState(6002);
        } else if (e instanceof FileStateException) {
            result.setState(6003);
        } else if (e instanceof FileUploadIOException) {
            result.setState(6004);
        }
        return result;
    }

    /**
     * 获取session的uid对象
     * @param session
     * @return 当前登录的用户的uid的值
     */
    protected  final  Integer getuidFromSession(HttpSession session){
      return   Integer.valueOf(session.getAttribute("uid")
                        .toString());
    }

    /**
     * 获取当前登录的username
     * @param session
     * @return 当前登录的用户名
     */
    protected  final  String getUsernameFromSession(HttpSession session){
        return   session.getAttribute("username").toString();
    }
}
